var myApp = sy.getModule('myApp',[]);
var data = '';
myApp.controller('MyController', function($scope,$http) {
	$scope.data = {};
	$scope.cartList = {};
	$scope.init = function(){
		if(sessionStorage.cartRestaurant == undefined){
			location.href = sy.path + "/m/store/diancan/"+_id;
		}
		if(sessionStorage.cartList == undefined || sessionStorage.cartList == '{}'){
			location.href = sy.path + "/m/store/diancan/"+_id;
		}
		var cart = sy.stringToJSON(sessionStorage.cartRestaurant);
		var cartList = sy.stringToJSON(sessionStorage.cartList);
		$scope.cartList = cartList;
		var dataJson = [];
		for(var item in cartList){
            var obj=cartList[item];
            dataJson.push({id:obj.id,num:obj.num});
	    } 
		data = sy.jsonToString(dataJson);
		console.log(data);
		sy.openLoad();
		$http.post(sy.path + "/m/order/diancan", {
			storeId:cart.storeId,data:data
		}).success(function(data) {
			sy.closeLoad();
			if (data == "sessionOut") location.href = sy.path + "/m/login";
			if(data.code == 0){
				$scope.data = data;
			}else{
				sy.alert(data.msg);
			}
		}).error(function(data, status) {
			sy.closeLoad();
			sy.alert('数据请求失败');
		});
	}
	$scope.init();
	
	$scope.goSubmit = function(payType){
		
		sy.openLoad();
		$http.post(sy.path + "/m/order/addDiancanOrder", {
			storeId : $scope.data.storeId,remark:$scope.remark,payType:payType,data:data,couponId:$scope.data.coupon==undefined?'':$scope.data.coupon.id
		}).success(function(data) {
			sy.closeLoad();
			if (data == "sessionOut") location.href = sy.path + "/m/login";
			if(data.code == 0){
				sessionStorage.removeItem("cartRestaurant");
				sessionStorage.removeItem("cartList");
				location.href = sy.path + "/m/order/"+data.data.msg;
			}else{
				sy.alert(data.msg);
			}
		}).error(function(data, status) {
			sy.closeLoad();
			sy.alert('数据请求失败');
		});
	}
	
	
});


